package top.ddandang.blog.service.impl;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import top.ddandang.blog.model.pojo.Message;
import top.ddandang.blog.mapper.MessageMapper;
import top.ddandang.blog.model.query.PageBean;
import top.ddandang.blog.model.vo.MessageRequestVo;
import top.ddandang.blog.model.vo.MessageVo;
import top.ddandang.blog.service.MessageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.dozer.Mapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author D
 * @since 2020-06-24
 */
@Service
public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message> implements MessageService {

    @Resource
    private MessageMapper messageMapper;

    @Resource
    private Mapper dozerMapper;

    @Override
    public void insertMessage(MessageRequestVo messageRequestVo) {
        Message message = dozerMapper.map(messageRequestVo, Message.class);
        while (true) {
            if(messageMapper.insert(message) == 1) {
                break;
            }
        }
    }

    @Override
    public Map<String, Object> listMessageVo(PageBean pageBean) {

        Map<String, Object> map = new HashMap<>();

        PageHelper.startPage(pageBean.getPageNo(), pageBean.getPageSize());
        List<MessageVo> messageVoList = messageMapper.listMessageVo();

        PageInfo<MessageVo> pageInfo = new PageInfo<> (messageVoList);
        long total = pageInfo.getTotal();
        map.put("messageVoList", messageVoList);
        map.put("total", total);

        return map;
    }

    @Override
    public int updateTopping(Integer messageId, Boolean top) {
        UpdateWrapper<Message> updateWrapper = new UpdateWrapper<Message>()
                .eq("id", messageId)
                .set("topping", top);
        return messageMapper.update(null, updateWrapper);
    }

    @Override
    public int deleteMessage(Integer messageId) {
        return messageMapper.deleteById(messageId);
    }
}
